package impl

import (
	"context"

	"gitee.com/go-course/go11/devcloud-mini/mcenter/apps/endpoint"
	"go.mongodb.org/mongo-driver/bson"
)

func (i *impl) RegistryEndpoints(ctx context.Context, in *endpoint.EndpointSpecSet) (
	*endpoint.EndpointSet, error) {
	// 清除掉service功能列表
	// sevice in ();
	_, err := i.col.DeleteMany(ctx, bson.M{"service": bson.M{"$in": in.Service()}})
	if err != nil {
		return nil, err
	}

	// 再重新创建
	set := endpoint.NewEndpointSet(in)

	_, err = i.col.InsertMany(ctx, set.ToMongoDocs())
	if err != nil {
		return nil, err
	}

	return set, nil
}
